<template>
	<view class="community">
		<view class="uni_header"></view>
		<view class="operating_box" v-if="list.length>0" v-for="(info,index) in list" :key="index" style="margin-top:-80rpx;margin-bottom:20rpx;">
			<view class="operating_header" style="border-bottom:2rpx dotted grey;">
				<view class="operating_header_left">
					<text class="operating_header_text padding-left-sm">{{info.sub_phone_number}}</text>
				</view>
				<view class="operating_header_right" @tap="del(info.sub_phone_number)">
					<view class="cu-avatar round bg-red text-xl">--</view> 
				</view>
			</view>
			<view class="cu-form-group" style="border-bottom:2rpx dotted grey;">
				<view class="title">账户权限</view>
				<view @tap="setAuth(info.sub_phone_number)">点击设置<text class="cuIcon-right"></text></view>
			</view>
			<view class="text-center margin-top-sm">
				<button style="width: 160rpx;" class="cu-btn bg-blue" @tap="addinfo(info.sub_phone_number)">保存</button>
			</view>
		</view>
		<view v-if="list.length==0" class="margin-top-lg text-center">
			<image src='../../../common/img/no_data.png' style="width:275rpx;height:312rpx;"></image>
		</view>
		<view v-if="list.length>0" class="text-black text-df text-center bg-white padding-sm padding-left-lg be-gray">
			{{loadingTxt}} 共({{list.length}}条记录)
		</view>
		<button class="cu-btn block bg-blue lg foot" @tap="addnewinfo">
			<text class="cuIcon-roundadd margin-right-xs"></text>新建账户</button>
	</view>
</template>

<script>
	let page=1,timer=null
	export default {
		data() {
			return {
				loadingTxt:'加载更多',
				list:[],
				totle:0,
				info:{
					_id:'',//	字符串	ID
					sub_phone_number:'',//	字符串	子帐号
				},
				isshow:false,
				checkvalue:'',
				checkguid:'',
				sjhm:'',
				phone:'',//设置权限后返回的手机号码
				type:""
			};
		},
		onLoad(options) {
			this.checkvalue=options.value;
			this.checkguid=options.checkguid;
		},
		onShow() {
		   page=1;
		   this.getList();
		},
		methods: {
			setAuth(sjhm) {
				uni.navigateTo({
					url:"/pages/community/commaccount/setauth?phone="+sjhm
				})
			},
			addnewinfo() {
				uni.navigateTo({
					url:"/pages/community/commaccount/addaccount"
				})
			},
			getList(){
				let that=this;
				that.http.tokenreq({
					url: 'subaccount/v1/list',
					data: {
				    "pageindex":page,
					}
				}).then((res) => {
					let e=res.data;
					if(e.code==0 && e.success){
						this.list = e.rows;
						this.totle = e.total;
					}else{
						uni.showModal({
							content:e.message,
							showCancel:false,
						})
					}
				})
			},
			getmorelist(){
				this.loadingTxt='加载中'
				let that=this;
				that.http.tokenreq({
					url: 'subaccount/v1/list',
					data: {
					"pageindex":page,
					}
				}).then((res) => {
					uni.hideLoading();
					let e=res.data;
					if(e.code==0 && e.success){
						if(e.rows.length==0){
							that.loadingTxt="已经加载全部";
							return;
						}
						that.list=that.list.concat(e.rows);
						page++
					}else{
						uni.showModal({
							content:e.message,
							showCancel:false,
						})
					}
				})
			},
			onReachBottom() {
				//阻止重复加载
				if(timer !== null){
					clearTimeout(timer)
				}
				timer=setTimeout(()=>this.getmorelist(),500)
			},
			addinfo(sjhm){
				this.save(sjhm);
			},
			save(sjhm){
				let that=this;
				that.http.tokenreq({
					url:"subaccount/v1/savesub",
					data:{
						"phone_number":sjhm,
						"menu_code":this.checkvalue,
						"residential_quarters_guid":this.checkguid,
					}
				}).then((res) => {
					console.log("res="+JSON.stringify(res));
					let e=res.data;
					if(e.code==0 && e.success){
						uni.showModal({
							content: e.message,
							showCancel:false,
							success: function (res) {
								if (res.confirm) {
									page=1;
									this.getList();
								}
							}
						});
					}else{
						uni.showToast({
							icon: 'none',
						    title: e.message
						});
					}
				})
			},
			//删除子账号
			del(number){
				let that=this;
				uni.showModal({
					title: '提示',
					content: '确定要删除该信息吗?',
					success: function (res) {
						if (res.confirm) {
							that.suredel(number);
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			suredel(number){
				let that=this;
				uni.showLoading();
				that.http.tokenreq({
					url: 'subaccount/v1/del',
					data:{"phone_number":number}
				}).then((res) => {
					console.log("res="+JSON.stringify(res));
					let e=res.data;
					if(e.code==0 && e.success){
						uni.hideLoading();
						uni.showModal({
							content: e.message,
							showCancel:false,
							success: function (res) {
								if (res.confirm) {
									page=1;
									that.getList();
								}
							}
						});
					}else{
						uni.showModal({
							content:e.message,
							showCancel:false,
						})
					}
				})
			}
			
		},
	}
</script>

<style>
	.uni_header{
		height:120rpx;
	}
	.operating_header_text{
		color: #000000;
	}
	.foot{
		position: fixed;
		left: 0;
		bottom: 0;
		width: 100%;
	}
</style>
